package com.zaofan.mobile.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zaofan.bean.UserImage;
import com.zaofan.cache.CacheManage;
import com.zaofan.exception.ServiceException;
import com.zaofan.mobile.bean.UserFollow;
import com.zaofan.service.AccountService;
import com.zaofan.service.DynamicService;
import com.zaofan.service.UserFollowService;
import com.zaofan.service.UserImageService;
import com.zaofan.utils.JsonUtils;

/**
 *
 */
@WebServlet("/mobile/fans")
public class FansController extends BaseServlet {

	private static final long serialVersionUID = 5884947841079014459L;
	private DynamicService dynamicService = new DynamicService();
	private AccountService accountService = new AccountService();
	private UserImageService imageService = new UserImageService();
	private UserFollowService followService = new UserFollowService();

	/**
	 * 首页 发现接口
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void index(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			Map<String, Object> datas = new HashMap<>();
			datas.put("banner", CacheManage.fansBanner);
			datas.put("fans", dynamicService.findTop4Activity());
			datas.put("dynamic", dynamicService.findJingxuanDynamic(0, 10));
			JsonUtils.writeSuccessJson("查询成功", datas, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("查询失败", response);
		}
		JsonUtils.writeBeanFailJson("查询失败!!" + ",未知错误", response);
	}

	/**
	 * 用户 论坛信息
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void user_info(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			String userIds = request.getParameter("userId");
			String ids = request.getParameter("id");
			int userId = 0;
			int id;
			try {
				id = Integer.valueOf(ids);
			} catch (Exception e) {
				e.printStackTrace();
				JsonUtils.writeListFailJson("参数错误!!", response);
				return;
			}
			try {
				userId = Integer.valueOf(userIds);
			} catch (Exception e) {
				e.printStackTrace();
				JsonUtils.writeListFailJson("对不起,您还未登录,请重新登录试试!!", response);
				return;
			}
			
			Map<String, Object> datas = new HashMap<>();
			datas.put("info", accountService.getUserFansInfo(id));
			datas.put("dynamic", dynamicService.findDynamicByUserId("0", "100", id));
			datas.put("isFollow", followService.isFollow(userId, id));
			JsonUtils.writeSuccessJson("查询成功", datas, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("查询失败", response);
		}
		JsonUtils.writeBeanFailJson("查询失败!!" + ",未知错误", response);
	}

	/**
	 * 用户动态列表
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void user_dynamic(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			String userIds = request.getParameter("userId");
			String begins = request.getParameter("begin");
			String ends = request.getParameter("end");
			Integer begin = 0;
			Integer end = 10;
			int userId = 0;
			try {
				begin = Integer.valueOf(begins);
				end = Integer.valueOf(ends);
			} catch (Exception e) {
				e.printStackTrace();
				JsonUtils.writeListFailJson("分页参数错误!!", response);
				return;
			}
			try {
				userId = Integer.valueOf(userIds);
			} catch (Exception e) {
				e.printStackTrace();
				JsonUtils.writeListFailJson("对不起,您还未登录,请重新登录试试!!", response);
				return;
			}
			List<com.zaofan.mobile.bean.Dynamic> datas = dynamicService.findDynamicByUserId(begin + "", end + "",
					userId);
			JsonUtils.writeSuccessJson("查询成功", datas, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("查询失败", response);
		}
		JsonUtils.writeListFailJson("查询失败!!" + ",未知错误", response);
	}

	/**
	 * 用户图片列表
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void user_image(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			String userIds = request.getParameter("userId");
			String begins = request.getParameter("begin");
			String ends = request.getParameter("end");
			Integer begin = 0;
			Integer end = 10;
			int userId = 0;
			try {
				begin = Integer.valueOf(begins);
				end = Integer.valueOf(ends);
			} catch (Exception e) {
				e.printStackTrace();
				JsonUtils.writeListFailJson("分页参数错误!!", response);
				return;
			}
			try {
				userId = Integer.valueOf(userIds);
			} catch (Exception e) {
				e.printStackTrace();
				JsonUtils.writeListFailJson("对不起,您还未登录,请重新登录试试!!", response);
				return;
			}
			List<UserImage> datas = imageService.findByUserId(begin + "", end + "", userId);
			JsonUtils.writeSuccessJson("查询成功", datas, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("查询失败", response);
		}
		JsonUtils.writeListFailJson("查询失败!!" + ",未知错误", response);
	}

	/**
	 * 关注用户
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void follow_user(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			String userIds = request.getParameter("userId"); // 操作
			String otherIds = request.getParameter("id"); // 被关注人
			Integer otherId = 0;
			int userId = 0;
			try {
				userId = Integer.valueOf(userIds);
				otherId = Integer.valueOf(otherIds);
			} catch (Exception e) {
				e.printStackTrace();
				JsonUtils.writeListFailJson("对不起,您还未登录,请重新登录试试!!", response);
				return;
			}
			followService.followUser(userId, otherId);
			JsonUtils.writeSuccessJson("关注成功", "", response);
			return;
		} catch (ServiceException e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson(e.getMessage(), response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("关注失败", response);
		}
		JsonUtils.writeBeanFailJson("关注失败!!" + ",未知错误", response);
	}

	/**
	 * 取消关注用户
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void follow_cancel(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			String userIds = request.getParameter("userId"); // 操作
			String otherIds = request.getParameter("id"); // 被关注人
			Integer otherId = 0;
			int userId = 0;
			try {
				otherId = Integer.valueOf(otherIds);
				userId = Integer.valueOf(userIds);
			} catch (Exception e) {
				e.printStackTrace();
				JsonUtils.writeListFailJson("对不起,您还未登录,请重新登录试试!!", response);
				return;
			}
			followService.userCancelFollow(userId, otherId);
			JsonUtils.writeSuccessJson("关注成功", "", response);
			return;
		} catch (ServiceException e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson(e.getMessage(), response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("关注失败", response);
		}
		JsonUtils.writeBeanFailJson("关注失败!!" + ",未知错误", response);
	}

	/**
	 * 用户粉丝列表
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void user_fans(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			String userIds = request.getParameter("userId"); // 操作
			int userId = 0;
			String begins = request.getParameter("begin");
			String ends = request.getParameter("end");
			Integer begin = 0;
			Integer end = 10;
			try {
				begin = Integer.valueOf(begins);
				end = Integer.valueOf(ends);
			} catch (Exception e) {
				e.printStackTrace();
				JsonUtils.writeListFailJson("分页参数错误!!", response);
				return;
			}
			try {
				userId = Integer.valueOf(userIds);
			} catch (Exception e) {
				e.printStackTrace();
				JsonUtils.writeListFailJson("对不起,您还未登录,请重新登录试试!!", response);
				return;
			}
			List<UserFollow> follows = followService.getUserFans(userId, begin + "", end + "");
			JsonUtils.writeSuccessJson("查询成功", follows, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("查询失败", response);
		}
		JsonUtils.writeListFailJson("查询失败!!" + ",未知错误", response);
	}

	/**
	 * 用户关注列表
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void user_follow(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			String userIds = request.getParameter("userId"); // 操作
			int userId = 0;
			String begins = request.getParameter("begin");
			String ends = request.getParameter("end");
			Integer begin = 0;
			Integer end = 10;
			try {
				begin = Integer.valueOf(begins);
				end = Integer.valueOf(ends);
			} catch (Exception e) {
				e.printStackTrace();
				JsonUtils.writeListFailJson("分页参数错误!!", response);
				return;
			}
			try {
				userId = Integer.valueOf(userIds);
			} catch (Exception e) {
				e.printStackTrace();
				JsonUtils.writeListFailJson("对不起,您还未登录,请重新登录试试!!", response);
				return;
			}
			List<UserFollow> follows = followService.getUserFollow(userId, begin + "", end + "");
			JsonUtils.writeSuccessJson("查询成功", follows, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("查询失败", response);
		}
		JsonUtils.writeListFailJson("查询失败!!" + ",未知错误", response);
	}

}
